using WebapiTemplate.Infrastructure;
using WebapiTemplate.Infrastructure.BuildSerilog;
using WebapiTemplate.Infrastructure.Filter;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();


//使DateTime.now()为Sqlserver和Postgres通用并解决系统与Postgres时间相差8小时问题
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);


//Cors问题
builder.Services.AddCors(options =>
{
    options.AddPolicy(
        name: "myCors",
        builde =>
        {
            builde.WithOrigins("*", "*", "*").AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
        }
    );
});

//Serilog日志
builder.SerilogConfigure();

# region 这个区域相当于net sdk的3.1版本的Startup.cs
//依赖注入
builder.Services.AddControllers(options =>
{
    //注册全局异常过滤器
    options.Filters.Add(typeof(HttpGlobalExceptionFilter));
    //审计日志过滤
    options.Filters.Add(typeof(AuditLogActionFilter));
});

//获取DependencyInjection的配置
builder.Services.AddInfrastructure(builder.Configuration); //builder.Services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));或者像这样直接一个一个硬写
# endregion




var app = builder.Build(); //启动以上所有配置

app.UseCors("myCors");

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthentication(); //开启认证
app.UseAuthorization();

//种子数据
app.MigrateDatabase();

app.MapControllers();

app.Run();
